package com.clk.clksense;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;

public class Settings extends Activity {

	public final static int SUCCESS_RETURN_CODE = 1;

	// ----------------------------------------------------------------------------------
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
	}

	// ----------------------------------------------------------------------------------
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_settings, menu);
		return true;
	}

	// ----------------------------------------------------------------------------------
	public void saveClick(View view) {
		if (cf()) {
			Intent intent = new Intent();
			Bundle b = new Bundle();
			b.putString("TEXT", "cikildi");
			intent.putExtras(b);
			setResult(SUCCESS_RETURN_CODE, intent);
			finish();
		}
	}

	// ----------------------------------------------------------------------------------
	@SuppressLint("WorldReadableFiles")
	private boolean cf() {
		boolean isTheSame = false;
		try { // catches IOException below
			final String TESTSTRING = new String("Hello Android");

			// ##### Write a file to the disk #####
			/*
			 * We have to use the openFileOutput()-method the ActivityContext
			 * provides, to protect your file from others and This is done for
			 * security-reasons. We chose MODE_WORLD_READABLE, because we have
			 * nothing to hide in our file
			 */
			FileOutputStream fOut = openFileOutput("samplefile.txt", MODE_WORLD_READABLE);
			OutputStreamWriter osw = new OutputStreamWriter(fOut);

			// Write the string to the file
			osw.write(TESTSTRING);
			/*
			 * ensure that everything is really written out and close
			 */
			osw.flush();
			osw.close();

			// ##### Read the file back in #####

			/*
			 * We have to use the openFileInput()-method the ActivityContext
			 * provides. Again for security reasons with openFileInput(...)
			 */
			FileInputStream fIn = openFileInput("samplefile.txt");
			InputStreamReader isr = new InputStreamReader(fIn);
			/*
			 * Prepare a char-Array that will hold the chars we read back in.
			 */
			char[] inputBuffer = new char[TESTSTRING.length()];
			// Fill the Buffer with data from the file
			isr.read(inputBuffer);
			// Transform the chars to a String
			String readString = new String(inputBuffer);

			// Check if we read back the same chars that we had written out
			isTheSame = TESTSTRING.equals(readString);

			// WOHOO lets Celebrate =)
			Log.i("File Reading stuff", "success = " + isTheSame);

			Toast.makeText(Settings.this, "file W/R success = " + isTheSame, Toast.LENGTH_LONG).show();

		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
		return isTheSame;
	}
}
